package com.onepower.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.onepower.core.domain.request.SysRoleForm;
import com.onepower.core.domain.response.SysRoleInfo;
import com.onepower.core.modules.repository.entity.SysRole;
import com.onepower.core.modules.service.ISysRoleService;
import com.onepower.core.result.CommonResult;
import com.onepower.core.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author kin
 * @since 2018-08-01
 */
@RestController
@RequestMapping("/api/sys/role")
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    @RequestMapping(value = "page", method = RequestMethod.GET)
    public CommonResult page(@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                             @RequestParam(value = "pageSize", required = false, defaultValue = "20") int pageSize) {
        IPage<SysRole> roleList = sysRoleService.page(new Page<>(pageNum, pageSize));
        return ResultUtils.ok(roleList);
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public CommonResult list() {
        List<SysRole> roleList = sysRoleService.list(new QueryWrapper<SysRole>().eq("status",0));
        return ResultUtils.ok(roleList);
    }


    @RequestMapping("/info/{roleId}")
    public CommonResult info(@PathVariable("roleId") Integer roleId) {
        SysRoleInfo roleInfo = sysRoleService.getRoleInfo(roleId);
        return ResultUtils.ok(roleInfo);
    }

    @RequestMapping(value = "save", method = RequestMethod.POST)
    public CommonResult save(@RequestBody SysRoleForm sysRole) {
        sysRoleService.save(sysRole);
        return ResultUtils.ok();
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public CommonResult update(@RequestBody SysRoleForm sysRole) {
        sysRoleService.save(sysRole);
        return ResultUtils.ok();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CommonResult delete(@RequestBody Integer[] roleIds) {
        sysRoleService.removeByIds(Arrays.asList(roleIds));
        return ResultUtils.ok();
    }
}
